package com.scpii.universal.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.scpii.universal.constant.GenericConstants;
import com.scpii.universal.dto.ManageUserDto;
import com.scpii.universal.hibernate.dao.BackUserManageDao;
import com.scpii.universal.service.BackUserManageService;

/**
 * 
 * universalWeb
 * 
 * @author zhangqiang 2012-7-10下午05:21:48
 */
@Service
public class BackUserManageServiceImpl extends
		BaseServiceImpl<ManageUserDto, Integer> implements BackUserManageService {
	@Resource
	private BackUserManageDao backUserManageDao;

	@Resource
	public void setBaseDao(BackUserManageDao backUserManageDao) {
		super.setBaseDao(backUserManageDao);
	}
    
	/**
	 * 获取用户的条数
	 * @param condition
	 * @return
	 * @author zhangqiaing
	 * 2012-7-21下午02:13:04
	 */
	public Integer getCountById(ManageUserDto condition) {
		StringBuffer hql = new StringBuffer("SELECT new com.scpii.universal.dto.ManageUserDto(t1.intUserId) ");		
		hql.append(" from ManageUserTab t1 , ");
		hql.append("  AppTab t2 , ");
		hql.append("  EnterpriseTab t3 ");
		hql.append(" where t1.intEnterpId= t3.intEnterpId ");
		hql.append(" and t1.intAppId= t2.intAppId ");
		hql.append(" and t1.intAppId= :intAppId");
		hql.append(" and t1.intDeleteSign=:intDeleteSign ");	
		hql.append(" and t2.intDeleteSign=:intDeleteSign ");
		hql.append(" and t3.intDeleteSign=:intDeleteSign ");
		Map<String,Object> argsMap = new HashMap<String,Object>();	
		
		//查询条件
		if(condition!=null){
			argsMap.put("intAppId", condition.getIntAppId());
			if(condition.getVarEnterpName()!=null&&!"".equals(condition.getVarEnterpName())){
				hql.append(" and t3.varEnterpName like:varEnterpName");
				argsMap.put("varEnterpName", "%"+condition.getVarEnterpName()+"%");
			}
			if(condition.getVarAppName()!=null&&!"".equals(condition.getVarAppName())){
				hql.append(" and t3.varEnterpName like:varAppName");
				argsMap.put("varAppName", "%"+condition.getVarAppName()+"%");
			}
			if(condition.getVarLoginName()!=null&&!"".equals(condition.getVarLoginName())){
				hql.append(" and t1.varLoginName like:varLoginName");
				argsMap.put("varLoginName", "%"+condition.getVarLoginName()+"%");
			}
		}
		argsMap.put("intDeleteSign", GenericConstants.DELETE_SIGN_EXIST);
		
		List<ManageUserDto> list=backUserManageDao.hqlGetList(hql.toString(), argsMap);
		if(list!=null){
			return list.size();
		}else{
			return 0;
		}
	}

	/**
	 * 获取用户信息
	 * @param condition
	 * @param pageNo
	 * @param pageSize
	 * @return
	 * @author zhangqiaing
	 * 2012-7-21下午02:13:38
	 */
	public List<ManageUserDto> getEnterpriceList(ManageUserDto condition,
			Integer pageNo, Integer pageSize) {
		StringBuffer hql = new StringBuffer("SELECT new com.scpii.universal.dto.ManageUserDto(t1.intUserId, ");	
		hql.append(" t1.intEnterpId , ");
		hql.append(" t1.intAppId , ");
		hql.append(" t1.varLoginName , ");
		hql.append(" t1.varPassword , ");
		hql.append(" t1.varPhonenum , ");
		hql.append(" t1.varEmail , ");
		hql.append(" t1.varRoleType , ");
		hql.append(" t2.varAppName , ");
		hql.append(" t3.varEnterpName ) ");
		hql.append(" from ManageUserTab t1 , ");
		hql.append("  AppTab t2 , ");
		hql.append("  EnterpriseTab t3 ");
		hql.append(" where t1.intEnterpId= t3.intEnterpId ");
		hql.append(" and t1.intAppId= t2.intAppId ");
		hql.append(" and t1.intAppId= :intAppId");
		hql.append(" and t1.intDeleteSign=:intDeleteSign ");	
		hql.append(" and t2.intDeleteSign=:intDeleteSign ");
		hql.append(" and t3.intDeleteSign=:intDeleteSign ");
		Map<String,Object> argsMap = new HashMap<String,Object>();	
		//查询条件
		if(condition!=null){
			argsMap.put("intAppId", condition.getIntAppId());
			if(condition.getVarEnterpName()!=null&&!"".equals(condition.getVarEnterpName())){
				hql.append(" and t3.varEnterpName like:varEnterpName");
				argsMap.put("varEnterpName", "%"+condition.getVarEnterpName()+"%");
			}
			if(condition.getVarAppName()!=null&&!"".equals(condition.getVarAppName())){
				hql.append(" and t2.varAppName like:varAppName");
				argsMap.put("varAppName", "%"+condition.getVarAppName()+"%");
			}
			if(condition.getVarLoginName()!=null&&!"".equals(condition.getVarLoginName())){
				hql.append(" and t1.varLoginName like:varLoginName");
				argsMap.put("varLoginName", "%"+condition.getVarLoginName()+"%");
			}
		}
		argsMap.put("intDeleteSign", GenericConstants.DELETE_SIGN_EXIST);
		
		return backUserManageDao.findByHql(hql.toString(), pageNo, pageSize, argsMap);
	}
	/**
	 * 通过userId 获取用户信息
	 * @param intUserId
	 * @return
	 * @author zhangqiaing
	 * 2012-7-21下午03:23:54
	 */
	@Override
	public ManageUserDto getManageUserById(Integer intUserId) {
		StringBuffer hql = new StringBuffer("SELECT new com.scpii.universal.dto.ManageUserDto(t1.intUserId, ");	
		hql.append(" t1.intEnterpId , ");
		hql.append(" t1.intAppId , ");
		hql.append(" t1.varLoginName , ");
		hql.append(" t1.varPassword , ");
		hql.append(" t1.varPhonenum , ");
		hql.append(" t1.varEmail , ");
		hql.append(" t1.varRoleType , ");
		hql.append(" t2.varAppName , ");
		hql.append(" t3.varEnterpName ) ");
		hql.append(" from ManageUserTab t1 , ");
		hql.append("  AppTab t2 , ");
		hql.append("  EnterpriseTab t3 ");
		hql.append(" where t1.intEnterpId= t3.intEnterpId ");
		hql.append(" and t1.intAppId= t2.intAppId ");
		hql.append(" and t1.intDeleteSign=:intDeleteSign ");	
		hql.append(" and t2.intDeleteSign=:intDeleteSign ");
		hql.append(" and t3.intDeleteSign=:intDeleteSign ");
		hql.append(" and t1.intUserId=:intUserId ");
		Map<String,Object> argsMap = new HashMap<String,Object>();	
		
		argsMap.put("intDeleteSign", GenericConstants.DELETE_SIGN_EXIST);
		argsMap.put("intUserId", intUserId);
		List<ManageUserDto> list=backUserManageDao.hqlGetList(hql.toString(), argsMap);
		if(list!=null){
			return list.get(0);
		}else{
			return null;
		}
	}

	
}
